package com.lc.cehui.ui.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.UserCouponListApi;
import com.lc.cehui.http.response.UserCouponListBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 优惠券
 */
public final class CouponActivity extends AppActivity {

    @BindView(R.id.wsy_tv)
    TextView wsyTv;
    @BindView(R.id.ysy_tv)
    TextView ysyTv;
    @BindView(R.id.ygq_tv)
    TextView ygqTv;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    private BaseQuickAdapter mAdapter;
    private List<UserCouponListBean.DataBean> dataList=new ArrayList<>();
    private int mPage=1;
    private String type="1";
    private String store_id="";
    @Override
    protected int getLayoutId() {
        return R.layout.coupon_activity;
    }

    @Override
    protected void initView() {

          mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BaseQuickAdapter<UserCouponListBean.DataBean, BaseViewHolder>(R.layout.item_coupon, dataList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, UserCouponListBean.DataBean bean) {
             baseViewHolder.setText(R.id.tv_money,bean.getRed_price()+"");
                baseViewHolder.setText(R.id.tv_manjian,"满"+bean.getShold_price()+"可用");
                baseViewHolder.setText(R.id.tv_type,bean.getName());
                baseViewHolder.setText(R.id.tv_date,"领劵后"+bean.getDay()+"天有效");
                baseViewHolder.setText(R.id.tv_youxiaodate,"有效期至"+bean.getEnd_time());
                if (type.equals("1")){
                    baseViewHolder.setBackgroundResource(R.id.tv_to_make,R.drawable.juxing_yuanjiao_blue)     ;
                    baseViewHolder.setText(R.id.tv_to_make,"去使用");
                }else if (type.equals("2")){
                    baseViewHolder.setBackgroundResource(R.id.tv_to_make,R.drawable.juxing_yuanjiao_ceff);
                    baseViewHolder.setText(R.id.tv_to_make,"已使用");
                }else {
                    baseViewHolder.setBackgroundResource(R.id.tv_to_make,R.drawable.juxing_yuanjiao_c1c1c1);
                    baseViewHolder.setText(R.id.tv_to_make,"已过期");
                }

            }
        };
        mRecyclerView.setAdapter(mAdapter);
        showEmptyView(mAdapter);

    }

    @Override
    protected void initData() {
        coupon(mPage,type,store_id);

        mAdapter.setNewData(dataList);
    }


    private void coupon(int mPage,String type,String store_id ) {
        EasyHttp.post(this)
                .api(new UserCouponListApi().setUserid(SPUtil.get(this,IntentKey.USERID,0)).setPage(mPage).setType(type).setStore_id(store_id))
                .request(new HttpCallback<UserCouponListBean>(this) {

                    @Override
                    public void onSucceed(UserCouponListBean data) {

                        if (data.getCode().equals("200")) {
                            dataList.clear();
                            dataList.addAll(data.getData());
                            mAdapter.setNewData(dataList);
                            mAdapter.notifyDataSetChanged();
                        }

                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.wsy_tv, R.id.ysy_tv, R.id.ygq_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wsy_tv:
                type="1";
                coupon(mPage,type,store_id);
                break;
            case R.id.ysy_tv:
                type="2";
                coupon(mPage,type,store_id);
                break;
            case R.id.ygq_tv:
                type="3";
                coupon(mPage,type,store_id);
                break;
        }
    }


}